package com.chengzhi.framework.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ReflectionUtil {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class); 
	/**
	 * 创建实例
	 */
	public static Object newInstace(Class<?> cls){
		Object isntace;
		try {
			isntace=cls.newInstance();
		} catch (Exception e) {
			LOGGER.error("new instance failure",e);
			throw new RuntimeException(e);
		}
		return isntace;
	}
	/**
	 * 调用方法
	 */
	public static Object invokeMethod(Object obj,Method method,Object...args){
		Object result;
		try {
			method.setAccessible(true);
			result = method.invoke(obj, args);
		} catch (Exception e) {
			LOGGER.error("invoke method failure",e);
			throw new RuntimeException(e);
		} 
		return result;
	}
	/**
	 * 设置成员变量的值
	 */
	public static void setFileld(Object object,Field field,Object value){
		try {
			field.setAccessible(true);
			field.set(object, value);
		} catch (Exception e) {
			LOGGER.error("set field failure", e);
			throw new RuntimeException(e);
		}
	}
}
